﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LdCms.Web.Services
{
    using LdCms.EF.DbModels;
    using LdCms.IBLL.Sys;
    using LdCms.IBLL.Institution;
    using LdCms.IBLL.Member;
    using LdCms.Common.Json;
    using LdCms.Common.Time;
    using LdCms.Common.Enum;
    /// <summary>
    /// 
    /// </summary>
    public partial class BaseApiManager:IBaseApiManager
    {
        private readonly IApiRecordManager ApiRecordManager;
        private readonly IInterfaceAccountService InterfaceAccountService;
        private readonly IInterfaceAccessTokenService InterfaceAccessTokenService;
        private readonly IStaffService StaffService;
        private readonly IStaffAccessTokenService StaffAccessTokenService;
        private readonly IAccountService AccountService;
        private readonly IAccountAccessTokenService AccountAccessTokenService;
        private readonly IOperatorService OperatorService;
        public BaseApiManager(IApiRecordManager ApiRecordManager, IInterfaceAccountService InterfaceAccountService, IInterfaceAccessTokenService InterfaceAccessTokenService, IStaffService StaffService, IStaffAccessTokenService StaffAccessTokenService, IAccountService AccountService, IAccountAccessTokenService AccountAccessTokenService, IOperatorService OperatorService)
        {
            this.ApiRecordManager = ApiRecordManager;
            this.InterfaceAccountService = InterfaceAccountService;
            this.InterfaceAccessTokenService = InterfaceAccessTokenService;
            this.StaffService = StaffService;
            this.StaffAccessTokenService = StaffAccessTokenService;
            this.AccountService = AccountService;
            this.AccountAccessTokenService = AccountAccessTokenService;
            this.OperatorService = OperatorService;
        }
        public int SystemID = BaseSystemConfig.SystemID;
        public bool IsPermission(string companyId, string staffId, string functionId)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(functionId))
                    return false;
                bool result = OperatorService.VerifyOperatorPermissionPro(SystemID, companyId, staffId, functionId);
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


        public long SaveLogs(string token)
        {
            return ApiRecordManager.Save(token);
        }
        public long SaveLogs(string token, string sParams)
        {
            return ApiRecordManager.Save(token, sParams);
        }
        public long SaveLogs(string token, object formValue)
        {
            return ApiRecordManager.Save(token, formValue);
        }
        public bool UpdateLogs(long id, string result, bool state)
        {
            return ApiRecordManager.Update(id, result, state);
        }


        public bool IsUuid(string uuid)
        {
            try
            {
                if (string.IsNullOrEmpty(uuid))
                    return false;
                if (uuid.Length != 32)
                    return false;
                return InterfaceAccountService.VerifyInterfaceAccountByUuIDPro(SystemID, uuid);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool IsToken(string token)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(token))
                    return false;
                if (token.Length != 128)
                    return false;
                int timestamp = TimeHelper.GetUnixTimestampTen();
                return InterfaceAccessTokenService.VerifyInterfaceAccessTokenPro(token, timestamp);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool IsAccessToken(string accessToken)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(accessToken))
                    return false;
                int timestamp = TimeHelper.GetUnixTimestampTen();
                if (accessToken.Length == (int)ParamEnum.TokenType.Member)
                    return AccountAccessTokenService.VerifyAccessTokenPro(accessToken, timestamp);
                else
                    return StaffAccessTokenService.VerifyStaffAccessTokenPro(accessToken, timestamp);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


        public Ld_Sys_InterfaceAccount GetSysInterfaceAccount(string token)
        {
            try
            {
                int tokenLength = token.Length;
                switch (tokenLength)
                {
                    case (int)ParamEnum.TokenType.AppID:
                        return InterfaceAccountService.GetInterfaceAccountByAppIDPro(SystemID, token);
                    case (int)ParamEnum.TokenType.Uuid:
                        return InterfaceAccountService.GetInterfaceAccountByUuidPro(SystemID, token);
                    case (int)ParamEnum.TokenType.Token:
                        return InterfaceAccountService.GetInterfaceAccountByTokenPro(SystemID, token);
                    default:
                        return null;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public Ld_Sys_InterfaceAccount GetSysInterfaceAccount(string companyId, string account)
        {
            try
            {
                return InterfaceAccountService.GetInterfaceAccountPro(SystemID, companyId, account);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public Ld_Sys_InterfaceAccount GetSysInterfaceAccountByAppId(string appid)
        {
            try
            {
                return InterfaceAccountService.GetInterfaceAccountByAppIDPro(SystemID, appid);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public Ld_Sys_InterfaceAccount GetSysInterfaceAccountByUuid(string uuid)
        {
            try
            {
                return InterfaceAccountService.GetInterfaceAccountByUuidPro(SystemID, uuid);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public Ld_Sys_InterfaceAccount GetSysInterfaceAccountByToken(string token)
        {
            try
            {
                return InterfaceAccountService.GetInterfaceAccountByTokenPro(SystemID, token);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


        public Ld_Institution_Staff GetInstitutionStaff(string companyId, string staffId)
        {
            try
            {
                return StaffService.GetStaffPro(SystemID, companyId, staffId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public Ld_Institution_Staff GetInstitutionStaffByAccessToken(string accessToken)
        {
            try
            {
                return StaffService.GetStaffByAccessTokenPro(SystemID, accessToken);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public Ld_Institution_Staff GetInstitutionStaffByRefreshToken(string refreshToken)
        {
            try
            {
                return StaffService.GetStaffByRefreshTokenPro(SystemID, refreshToken);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


        public Ld_Member_Account GetMemberAccount(string companyId,string memberId)
        {
            try
            {
                return AccountService.GetAccountPro(SystemID, companyId, memberId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public Ld_Member_Account GetMemberAccountByAccessToken(string accessToken)
        {
            try
            {
                return AccountService.GetAccountByAccessTokenPro(SystemID, accessToken);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public Ld_Member_Account GetMemberAccountByRefreshToken(string refreshToken)
        {
            try
            {
                return AccountService.GetAccountByRefreshTokenPro(SystemID, refreshToken);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }



    }
}
